package com.lavida.project.system.emp.controller;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.UUID;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.lavida.common.utils.security.ShiroUtils;
import com.lavida.framework.web.controller.BaseController;
import com.lavida.project.common.DownLoadUtils;
import com.lavida.project.common.FileUpload;
import com.lavida.project.system.attendance.domain.Attendances;
import com.lavida.project.system.attendance.service.AttendanceService;
import com.lavida.project.system.emp.domain.Emp;
import com.lavida.project.system.emp.domain.Empleave;
import com.lavida.project.system.emp.domain.Roster;
import com.lavida.project.system.emp.service.EmpService;
import com.lavida.project.system.emp.service.EmpleaveService;
import com.lavida.project.system.emp.service.RosterService;
import com.lavida.project.system.review.domain.Review;
import com.lavida.project.system.review.service.ReviewService;
/**
 * 	入职交接单
 * @author abh67
 *
 */
@Controller
@RequestMapping("system/empentry")
public class EmpEntryController extends BaseController{
	private String prefix = "system/emp";
	@Autowired
	private EmpService empService;
	
	@Autowired
	private RosterService rosterService;
	
	@Autowired
	private EmpleaveService empleaveService;
	
	@RequestMapping("/fasong")
	public String empentry(HttpServletRequest request,Integer job_id) {
		Emp emp = empService.selectByJob_id(job_id);
		if(emp != null) {
			request.setAttribute("emp", emp);	
		}
		return prefix+"/empentry";
	}
	/*@RequestMapping("/leave")
	public String empentry2(HttpServletRequest request,Integer job_id) {
		Emp emp = empService.selectByJob_id(job_id);
		Roster roster = rosterService.getRoster(job_id);
		
		if(emp != null) {
			
			request.setAttribute("emp", emp);
			request.setAttribute("Roster", roster);
		}
		return prefix+"/empentry2";
	}*/
	//文件上传
		@RequestMapping("/update")
		public String update(
				MultipartFile Befoleavefile1,
				MultipartFile academiccer1,
				MultipartFile procer1,
				MultipartFile copyfile1,
				MultipartFile laborhand1,
				MultipartFile mereport1,
				MultipartFile sopaytcer1,
				MultipartFile recordpro1,
				MultipartFile cardpicture1,
				MultipartFile card1,
				MultipartFile banknum1,
				MultipartFile leavefile1, 
				MultipartFile laborcontract1,
				MultipartFile otherdata1,
				MultipartFile empphoto1,
				int job_id,
				HttpServletRequest request,
				Emp emp
				) {
			Emp emp1= empService.selectEmpByempno(job_id);
			System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>"+emp1);
			emp.setBefoleavefile(Befoleavefile1.isEmpty() ? emp1.getBefoleavefile() : getFilenameUUID(Befoleavefile1,request));
			emp.setAcademiccer(academiccer1.isEmpty() ? emp1.getAcademiccer() : getFilenameUUID(academiccer1 , request));
			emp.setProcer(procer1.isEmpty() ? emp1.getProcer() : getFilenameUUID(procer1, request));
			emp.setCopyfile(copyfile1.isEmpty() ? emp1.getCopyfile() :getFilenameUUID(copyfile1, request));
			emp.setLaborhand(laborhand1.isEmpty() ? emp1.getLaborhand() : getFilenameUUID(copyfile1 , request));
			emp.setMereport(mereport1.isEmpty() ? emp1.getMereport() : getFilenameUUID(mereport1, request));
			emp.setSopaytcer(sopaytcer1.isEmpty() ? emp1.getSopaytcer() : getFilenameUUID(sopaytcer1, request));
			emp.setRecordpro(recordpro1.isEmpty() ? emp1.getSopaytcer() : getFilenameUUID(recordpro1, request));
			emp.setCardpicture(cardpicture1.isEmpty() ? emp1.getCardpicture():getFilenameUUID(cardpicture1, request));
			emp.setCard(card1.isEmpty() ? emp1.getCard() : getFilenameUUID(card1, request));
			emp.setBanknum(banknum1.isEmpty() ? emp1.getBanknum() :getFilenameUUID(banknum1, request));
			emp.setLeavefile(leavefile1.isEmpty() ? emp1.getLeavefile() : getFilenameUUID(leavefile1, request));
			emp.setLaborcontract(
					laborcontract1.isEmpty() ? emp1.getLaborcontract() : getFilenameUUID(laborcontract1, request));
			emp.setOtherdata(otherdata1.isEmpty() ? emp1.getOtherdata() : getFilenameUUID(otherdata1 , request));
			emp.setEmpphoto(empphoto1.isEmpty() ? emp1.getEmpphoto() : getFilenameUUID(empphoto1, request));
			empService.updateEmp(emp);
			Roster roster = rosterService.getRoster(job_id);
			return "redirect:fasong?job_id="+job_id;
		}
		//文件下载
		@RequestMapping("/download")
		public void download(
				String filename,
				HttpServletRequest request,
				HttpServletResponse response
				) {
			String string = request.getHeader("User-Agent");
			String Uploadfilename = null;
			//System.out.println("filename>>>>>>>>>>>>"+filename);
			try {
				
				Uploadfilename = DownLoadUtils.getFileName(string,String.format(filename));
				response.setHeader("Content-disposition", "attachment;filename=" + Uploadfilename);
				String fileVmPath = FileUpload.filePath(request);

				ServletOutputStream s = response.getOutputStream();
				BufferedInputStream bufferedInputStream = new BufferedInputStream(
						new FileInputStream(String.format("%s/%s", fileVmPath, filename)));
				int a;
				byte[] bytes = new byte[1024];
				while ((a = bufferedInputStream.read(bytes)) != -1) {
					s.write(bytes, 0, a);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		private String getFilenameUUID(MultipartFile file,HttpServletRequest request) {	
			
			 String path=FileUpload.filePath(request);
		     String originalFilename=file.getOriginalFilename(); 
		     
		     String uuid=UUID.randomUUID().toString().replace("-","");
		     String filename=uuid+"_"+originalFilename;
		     try {
		    	 file.transferTo(new File(path,filename));
		     }catch (Exception e) {
				// TODO: handle exception
			}
		     return filename;
		}
	}

